﻿using Shell.DomainDot;
using Framework.Poco;
using System;
using System.Linq;
using System.Collections.Generic;

namespace Framework.Domain
{
    /// <summary>
    /// 友情链接上下文
    /// </summary>
    public class FriendlyLinkContext : DomainDotBase
    {
        /// <summary>
        /// 获取或设置所有的友情链接
        /// </summary>
        public WithAll<FriendlyLink> FriendlyLinks { private get; set; }

        /// <summary>
        /// 获取所有的友情链接
        /// </summary>
        public IEnumerable<FriendlyLink> GetAllFriendlyLinks()
        {
            return this.FriendlyLinks;
        }

        /// <summary>
        /// 获取全局友情链接
        /// </summary>
        public IEnumerable<FriendlyLink> GetGlobalAdvertising()
        {
            return this.FriendlyLinks;
        }

        /// <summary>
        /// 创建友情链接
        /// </summary>
        public void Create(FriendlyLink friendlyLink)
        {
            friendlyLink.Created = DateTime.Now;
            friendlyLink.CategoryOrAgencyGroup = "全局友情链接";
            var entity = this.FriendlyLinks.Create();
            PocoUtils.AssignProperty<FriendlyLink>(friendlyLink, entity, typeof(Guid));
            this.FriendlyLinks.Submit();
        }

        /// <summary>
        /// 友情链接删除
        /// </summary>
        public void Delete(Guid identity)
        {
            var original = this.GetAllFriendlyLinks().SingleOrDefault(o => o.Id == identity);
            if (original != null)
            {
                this.FriendlyLinks.Delete(original.Id);
                this.FriendlyLinks.Submit();
            }
        }
    }
}
